<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增商品</title>
    <script src="../modules/js/common.js"></script>
    <style type="text/css">
        select{width:180px;height: 20px}
    </style>
</head>
<body>
<div align="center">
    <h1>新增商品</h1><br>
    商品名称:<input id="name"><br><br>
    商品类型:<select id="type">
                <option value="电脑">电脑</option>
                <option value="手机">手机</option>
            </select><br><br>
    商品价格:<input id="price"><br><br>
    图片地址:<input id="image"><br><br>
    卖点:<input id="sellPoint"><br><br>
    数量:<input id="number"><br><br>
    <button  onclick="AddItem()">确认</button>
    <button  onclick="goBack()">返回</button>
</div>

<script>
    function AddItem(){
        let name=$("#name").val();
        let type=$("#type").val();
        let price=$("#price").val()*100;
        let image=$("#image").val();
        let sellPoint=$("#sellPoint").val();
        let number=$("#number").val();
        let createdUser=$.cookie("username");
        let valid=1;
        if(name==""||price==""){
            alert("名称,价格不能为空");
            return;
        }
        let data={name:name,type:type,price:price,image:image,sellPoint:sellPoint,
            createdUser:createdUser,number:number,valid:valid};
        $.ajax({
            method:"post",data:JSON.stringify(data),contentType:"application/json",
            url:"http://localhost:8080/item/",
            crossDomain: true,xhrFields: {withCredentials: true},
            success(result) {
                if(result.state==1){
                    alert("新增成功");
                    goBack();
                }else{
                    alert("新增失败");
                }
            }
        })
    }


    function goBack(){
        window.location.href="http://localhost:8080/manage/manage.html";
    }
</script>
</body>
</html>